package com.jwt.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class JWTUtils {
     private static final String MMKEY="578DAJHDQ!@&$%^!Q*@#$%&";
    /*
    *  生成token header.payload.sing
    * */
    public static String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance(); //日历
        instance.add(Calendar.DATE,7);  //在7天过后过期
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v)->{
                 builder.withClaim(k,v);
                }
                );
        String token = builder
                .withExpiresAt(instance.getTime())  //设置过期时间
                .sign(Algorithm.HMAC256(MMKEY));//签名
        return token;
    }

    /*
    *  验证token 合法性 和获取方法
    * */
    public static DecodedJWT verify(String token){
     return  JWT.require(Algorithm.HMAC256(MMKEY)).build().verify(token);
    }


}
